package com.hcc.gateway.common.utils;

import cn.hutool.core.util.StrUtil;
import com.hcc.gateway.common.domain.DubboReq;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DubboUtils
 *
 * @author hushengjun
 * @date 2022/1/9
 */
public class DubboUtils {

    private static final String backup = "backup";
    private static final String version = "version";
    private static final String group = "group";
    private static final String timeout = "timeout";
    private static final String retries = "retries";

    private DubboUtils() {}

    public static DubboReq parseUrl(String url) {
        URI uri = URI.create(url);
        DubboReq req = new DubboReq();
        req.setRegCenterUrl(uri.getScheme() + "://" + uri.getHost() + (uri.getPort() == -1 ? "" : ":" + uri.getPort()));

        String path = uri.getPath();
        req.setInterfaceName(StrUtil.subBetween(path, "/", "/"));
        req.setMethodName(StrUtil.subAfter(path, "/", true));
        String query = uri.getQuery();
        if (StrUtil.isEmpty(query)) {
            return req;
        }
        Map<String, String> queryMap = new HashMap<>();
        List<String> split = StrUtil.split(query, "&");
        for (String s : split) {
            String[] ss = StrUtil.splitToArray(s, "=");
            queryMap.put(ss[0], ss[1]);
        }
        if (StrUtil.isNotEmpty(queryMap.get(backup))) {
            req.setRegCenterUrl(req.getRegCenterUrl() + "?backup=" + queryMap.get(backup));
        }
        if (StrUtil.isNotEmpty(queryMap.get(version))) {
            req.setVersion(queryMap.get(version));
        }
        if (StrUtil.isNotEmpty(queryMap.get(group))) {
            req.setGroup(queryMap.get(group));
        }
        if (StrUtil.isNotEmpty(queryMap.get(timeout))) {
            req.setTimeout(Integer.valueOf(queryMap.get(timeout)));
        }
        if (req.getTimeout() != null && StrUtil.isNotEmpty(queryMap.get(retries))) {
            req.setRetries(Integer.valueOf(queryMap.get(retries)));
        }

        return req;
    }

    public static String mergeUrl(DubboReq req) {
        if (req == null) {
            return null;
        }

        StringBuilder query = new StringBuilder();
        String regCenterUrl = req.getRegCenterUrl();
        if (regCenterUrl.contains("?" + backup)) {
            query.append(regCenterUrl.substring(regCenterUrl.indexOf("?" + backup))).append("&");
            regCenterUrl = regCenterUrl.substring(0, regCenterUrl.indexOf("?" + backup));
        } else {
            query.append("?");
        }
        if (StrUtil.isNotEmpty(req.getVersion())) {
            query.append(version).append("=").append(req.getVersion()).append("&");
        }
        if (StrUtil.isNotEmpty(req.getGroup())) {
            query.append(group).append("=").append(req.getGroup()).append("&");
        }
        if (req.getTimeout() != null) {
            query.append(timeout).append("=").append(req.getTimeout()).append("&");
        }
        if (req.getTimeout() != null && req.getRetries() != null) {
            query.append(retries).append("=").append(req.getRetries());
        }

        return regCenterUrl + "/" + req.getInterfaceName() + "/" + req.getMethodName() + query;
    }

}
